//
//  CZUserAccount.m
//  XinLang_OC
//
//  Created by WONG on 16/2/22.
//  Copyright © 2016年 cheuhui. All rights reserved.
//

#import "CZUserAccount.h"

#define plistPath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"account.plist"]

@implementation CZUserAccount

- (instancetype)initWithDict:(NSDictionary *)dict {
    if (self = [super init]) {
//        [self setValuesForKeysWithDictionary:dict];
        self.access_token = dict[@"access_token"];
        self.expires_in = [dict[@"expires_in"] doubleValue];
        self.uid = dict[@"uid"];
    }
    return self;
}
+ (instancetype)userWithDict:(NSDictionary *)dict {
    return [[self alloc] initWithDict:dict];
}
// 当kvc字典的key在模型中找不到对应的属性调用,避免报错
- (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *,id> *)keyedValues { }

- (void)setExpires_in:(NSTimeInterval)expires_in {
    _expires_in = expires_in;
    
    _expiresDate = [[NSDate alloc] initWithTimeIntervalSinceNow:_expires_in];
}

- (BOOL)userLogin {
    return [CZUserAccount loadUserAccount];
}

/**
 *  保存账号信息到沙盒
 */
- (void)saveAccout {
    [NSKeyedArchiver archiveRootObject:self toFile:plistPath];
}

/*
 项目中经常需要使用到CZUserAccount,如果每次调用loadUserAccount,都会去沙盒解档一个账户.这样是比较消耗性能.
 当没有账户,去沙盒解档一个.如果解档账户成功,将它保存在内存中,以后直接访问内存中的账户.可以提高性能
 */

/// 内存中的账户.
static CZUserAccount *userAccount = nil;

/**
 *  读取 保存在沙盒中的账号信息
 */
+ (CZUserAccount *)loadUserAccount {
    if (userAccount == nil) {
        userAccount = [NSKeyedUnarchiver unarchiveObjectWithFile:plistPath];
        NSLog(@"从沙盒中加载账户");
    }else {
        NSLog(@"从内存中获取到账户");
    }
    
    // 2.判断内存中是否有用账户,有账户,判断时间是否过期
    if (userAccount != nil && [userAccount.expiresDate compare:[NSDate date]] == NSOrderedDescending) {
        NSLog(@"有账户并且没有过期,是一个可用的账户");
        return userAccount;
    }
    return nil;
}
/**
 *  加载微博数据
 */
- (void)loadUserInfo {
    [[CZNetworkTool sharedHttpTool] loadUserInfo:^(id result, NSError *error) {
        if (error != nil || result == nil) {
            NSLog(@"加载用户信息失败error = %@",error);
            return;
        }
        //NSLog(@"加载用户信息成功result = %@",result);
        
        self.screen_name = result[@"screen_name"];
        self.avatar_large = result[@"avatar_large"];
        userAccount = self;
        // 保存
        [self saveAccout];
    }];
}


//告诉系统怎么去归档
- (void)encodeWithCoder:(NSCoder*)encoder
{
    [encoder encodeObject:self.access_token forKey:@"access_token"];
    [encoder encodeInteger:self.expires_in forKey:@"expires_in"];
    [encoder encodeObject:self.expiresDate forKey:@"expiresDate"];
    [encoder encodeObject:self.uid forKey:@"uid"];
    [encoder encodeObject:self.screen_name forKey:@"screen_name"];
    [encoder encodeObject:self.avatar_large forKey:@"avatar_large"];
}

//告诉系统怎么去解档
- (instancetype)initWithCoder:(NSCoder*)decoder
{
    self = [super init];
    if (self) {
        self.access_token = [decoder decodeObjectForKey:@"access_token"];
        self.expires_in = [decoder decodeIntegerForKey:@"expires_in"];
        self.expiresDate = [decoder decodeObjectForKey:@"expiresDate"];
        self.uid = [decoder decodeObjectForKey:@"uid"];
        self.screen_name = [decoder decodeObjectForKey:@"screen_name"];
        self.avatar_large = [decoder decodeObjectForKey:@"avatar_large"];
    }
    return self;
}




@end
